<?php 
require_once('dao/OffreAchatOptionFixeDao.php');

/**
 * Controleur pour la creation d'offre achat fixe
 */
if ($_SESSION['niveauAuth']=='1' or $_SESSION['niveauAuth']=='3' or $_SESSION['niveauAuth']=='5'or $_SESSION['niveauAuth']=='8') {	
	
$offres_vga_num = OffreAchatOptionFixeDao::GetByType('vga/num');
$offres_vga = OffreAchatOptionFixeDao::GetByType('vga');
$offres_voip = OffreAchatOptionFixeDao::GetByType('voip');
$offres_xdsl = OffreAchatOptionFixeDao::GetByType('xdsl');

//liste des pays fixe
$pays = OffreAchatOptionFixeDao::getAllPays();
	
//création d'une nouvelle offre achat fixe
if (isset($_POST['creation']))
{
if(isset($_POST['fixe'])){$fixe = $_POST['fixe'];}else{$fixe = NULL;}
if(isset($_POST['mobile'])){$mobile = $_POST['mobile'];}else{$mobile = NULL;}
if(isset($_POST['pays'])){$pays = $_POST['pays'];}else{$pays = NULL;}
	
$liste_incompat = implode(',',$_POST['incompatibilite']);

$requete = $bdd->prepare("INSERT INTO `ha_option_fixe` (`id`, `type`, `code_article`, `nom`, `prix_ha`, `temps_min`, `jours`, `horaires`,`fixe`,`mobile`,`international`, `incompatibilite`) VALUES ('', '".$_POST['type']."', '".$_POST['code_article']."',  '".$_POST['nom']."','".$_POST['prix_ha']."','".$_POST['temps_min']."','".$_POST['jours']."','".$_POST['horaires']."','".$fixe."','".$mobile."','".$pays."', '".$liste_incompat."') ");
$requete->execute() or exit(print_r($bdd->errorInfo()));

	
	// Redirection vers la page 
	if ($_POST['action'] == "creation"){
			// Pas de changement de page
			$success=true;
	} else if ($_POST['action'] == "achat-option-fixe-creation"){
			header("Location:achat-offre-fixe-creation.php?success=true");
	} else if ($_POST['action'] == "achat-option-fixe"){
		header("Location:achat-option-fixe.php?success=true");
	}
}

//$twig = new Twig_Environment($loader, array(
 //   'debug' => true,
//)); 
//$twig->addExtension(new Twig_Extension_Debug());
// a afficher dans le html: {{ debug() }}

echo $twig->render('achat-option-fixe-creation.html', 
		array(	'offres_vga_num' => $offres_vga_num,
				'offres_vga' => $offres_vga,
				'offres_voip' => $offres_voip,
				'offres_xdsl' => $offres_xdsl,
				'pays' => $pays,
				'message' => $message, 
				'error' => $error));

}
else {
	redirect("home-admin");
}
